package leecode.q202;

import java.util.HashSet;
import java.util.Set;

public class Solution {
    Set<Integer> set = new HashSet<>();

    public boolean isHappy(int n) {
        return deal(n);
    }

    private boolean deal(int n) {
        if (set.contains(n)) {
            return false;
        } else {
            set.add(n);
        }
        int sum = 0;
        while (n / 10 > 0) {
            int curGe = n % 10;
            n = n / 10;
            sum += curGe * curGe;
        }
        sum += n * n;
        if (sum == 1) {
            return true;
        } else {
            return deal(sum);
        }
    }
}
